﻿using System;
using System.Collections.Generic;

using Gonte.UI;
using Gonte.Wpf.Controls.ViewModel;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyWpfControlsTest
{
    [TestClass()]
    public class ViewModelInfoTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        #region Single child type tests

        [UserInterface(
        LabelProperty = "Name",
        IconUri = @"pack://application:,,/Images/Item.jpg")]
        public class ChildTreeItem
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }

        [UserInterface(
        LabelProperty = "Name",
        IconUri = @"pack://application:,,/Images/Root.jpg")]
        [UserInterfaceItem(PropertyName = "Children")]
        public class ParentTreeItem
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            private List<ChildTreeItem> _children = new List<ChildTreeItem>();

            public List<ChildTreeItem> Children
            {
                get { return _children; }
                set { _children = value; }
            }
        }

        [UserInterface(
        LabelProperty = "Name",
        IconUri = @"pack://application:,,/Images/Root.jpg")]
        [UserInterfaceItem(PropertyName = "Parents")]
        public class RootTreeItem
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            private List<ParentTreeItem> _parents = new List<ParentTreeItem>();

            public List<ParentTreeItem> Parents
            {
                get { return _parents; }
                set { _parents = value; }
            }
        }

        [TestMethod()]
        public void ViewModelInfoCreateInfoTest()
        {
            Type type = typeof(ParentTreeItem);
            ViewModelInfo viewModelInfo = ViewModelInfo.GetInfo(type);

            Assert.AreEqual("Name", viewModelInfo.LabelProperty);
            Assert.AreEqual("pack://application:,,/Images/Root.jpg", viewModelInfo.IconUri);
            Assert.AreEqual("pack://application:,,/Images/Root.jpg", viewModelInfo.ExpandedIconUri);
            Assert.AreEqual("pack://application:,,/Images/Root.jpg", viewModelInfo.CollapsedIconUri);

            Assert.AreEqual(1, viewModelInfo.ChildrenProperties.Length);
            Assert.AreEqual("Children", viewModelInfo.ChildrenProperties[0]);

            type = typeof(ChildTreeItem);
            viewModelInfo = ViewModelInfo.GetInfo(type);

            Assert.AreEqual("Name", viewModelInfo.LabelProperty);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.IconUri);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.ExpandedIconUri);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.CollapsedIconUri);

            Assert.IsNull(viewModelInfo.ChildrenProperties);
        } 

        #endregion

        #region Multiple children types tests

        [UserInterface(
        LabelProperty = "Name",
        IconUri = @"pack://application:,,/Images/Item.jpg")]
        public class ChildTreeItem2
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }

        [UserInterface(
        LabelProperty = "Name",
        CollapsedIconUri = @"pack://application:,,/Images/CollapsedRoot.jpg",
        ExpandedIconUri = @"pack://application:,,/Images/ExpandedRoot.jpg")]
        [UserInterfaceItem(PropertyName = "Children")]
        [UserInterfaceItem(PropertyName = "Children2")]
        public class RootTreeItem2
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            private List<ChildTreeItem> _children = new List<ChildTreeItem>();

            public List<ChildTreeItem> Children
            {
                get { return _children; }
                set { _children = value; }
            }

            private List<ChildTreeItem2> _children2 = new List<ChildTreeItem2>();

            public List<ChildTreeItem2> Children2
            {
                get { return _children2; }
                set { _children2 = value; }
            }
        }

        [TestMethod()]
        public void ViewModelInfoCreateInfoMultipleChildrenTypesTest()
        {
            Type type = typeof(RootTreeItem2);
            ViewModelInfo viewModelInfo = ViewModelInfo.GetInfo(type);

            Assert.AreEqual("Name", viewModelInfo.LabelProperty);
            Assert.IsNull(viewModelInfo.IconUri);
            Assert.AreEqual("pack://application:,,/Images/ExpandedRoot.jpg", viewModelInfo.ExpandedIconUri);
            Assert.AreEqual("pack://application:,,/Images/CollapsedRoot.jpg", viewModelInfo.CollapsedIconUri);

            Assert.AreEqual(2, viewModelInfo.ChildrenProperties.Length);
            Assert.AreEqual("Children", viewModelInfo.ChildrenProperties[0]);
            Assert.AreEqual("Children2", viewModelInfo.ChildrenProperties[1]);

            type = typeof(ChildTreeItem);
            viewModelInfo = ViewModelInfo.GetInfo(type);

            Assert.AreEqual("Name", viewModelInfo.LabelProperty);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.IconUri);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.ExpandedIconUri);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.CollapsedIconUri);

            Assert.IsNull(viewModelInfo.ChildrenProperties);

            type = typeof(ChildTreeItem2);
            viewModelInfo = ViewModelInfo.GetInfo(type);

            Assert.AreEqual("Name", viewModelInfo.LabelProperty);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.IconUri);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.ExpandedIconUri);
            Assert.AreEqual("pack://application:,,/Images/Item.jpg", viewModelInfo.CollapsedIconUri);

            Assert.IsNull(viewModelInfo.ChildrenProperties);
        }  

        #endregion
    }
}
